/**
 * @FileName: TitleBar.java
 * @Package:com.bdtop.dbl.ui.widgets
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-24
 * @version V1.0
 */

package com.bdtop.dbl.ui.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bdtop.dbl.R;

/**
 * @ClassName: TitleBar
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-24
 */
public class TitleBar extends RelativeLayout {

    private ImageButton mBackBtn;
    private TextView mTitleView;
    private LinearLayout mMenuContainer;
    private ImageView waitingFlag;

    private Activity mBelongsToActivity;

    public TitleBar(Context context) {
        super(context);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(getContext()).inflate(R.layout.title_bar, this);

        mBackBtn = (ImageButton) findViewById(R.id.back);
        mBackBtn.setOnClickListener(mOnClickListener);

        mTitleView = (TextView) findViewById(R.id.title);

        mMenuContainer = (LinearLayout) findViewById(R.id.menu_container);

        waitingFlag = (ImageView) findViewById(R.id.waiting_flag);
        waitingFlag.setVisibility(View.GONE);
    }

    public void setTitle(String title) {
        mTitleView.setText(title);
    }

    public void setTitle(int resId) {
        mTitleView.setText(resId);
    }

    
    public String getTitle() {
        return mTitleView.getText().toString();
    }

    public void addMenu(View view) {
        // mMenuContainer.removeAllViewsInLayout();
        // view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
        // LayoutParams.MATCH_PARENT));
        mMenuContainer.addView(view);
    }

    public void removeMenu(View view) {
        // mMenuContainer.removeAllViews();
        // mMenuContainer.removeAllViewsInLayout();
        mMenuContainer.removeView(view);
    }

    public void setBelongActivity(Activity activity) {
        mBelongsToActivity = activity;
    }

    public void setBackShowOrHidden(int visibility) {
        mBackBtn.setVisibility(visibility);
    }
    
    public void setBackListener (OnClickListener backClickListener) {
    	mBackBtn.setOnClickListener(backClickListener);
    }

    private OnClickListener mOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.back:
                    if (mBelongsToActivity != null) {
                        mBelongsToActivity.onBackPressed();
                    }
                    break;

                default:
                    break;
            }
        }
    };

    public void showWaitingFlag() {
        waitingFlag.setVisibility(View.VISIBLE);
        waitingFlag.startAnimation(
                AnimationUtils.loadAnimation(mBelongsToActivity, R.anim.rotate_forever));
    }

    public void hiddenWaitingFlag() {
        waitingFlag.setVisibility(View.GONE);
    }
}
